﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibGreen;
using System.Windows.Threading;

namespace WindowsFormsApplication1
{
	public partial class BuddyChatPanel : UserControl
	{
		public LibGreen.Contact con;
		public Dispatcher dis = Dispatcher.CurrentDispatcher;
		public BuddyChatPanel (Contact con)
		{
			InitializeComponent ();
			this.con = con;
			con.MessageReceived += con_MessageReceived;
		}

		private void con_MessageReceived (IDialog sess, LibGreen.Message message)
		{
			dis.BeginInvoke(DispatcherPriority.Input, new Action<LibGreen.Message>(MessageReceived), message);

		}
		private void MessageReceived (LibGreen.Message mes)
		{
			this.ChatText.AppendText (this.con.Alias + ": " + mes.Text + "\n");
		}

		private int isShifted = 0;
		private void UserNewText_KeyDown (object sender, KeyEventArgs e)
		{
			if (e.Shift)
				isShifted++;
		}

		private void UserNewText_KeyPress (object sender, KeyPressEventArgs e)
		{

			if ((e.KeyChar == '\n' || e.KeyChar == '\r') && isShifted <= 0)
				con.SendMessage (new LibGreen.Message () { Text = NewUserText.Text });
		}

		private void UserNewText_KeyUp (object sender, KeyEventArgs e)
		{
			if (e.Shift)
				isShifted--;
		}

		private void NewUserText_KeyPress (object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar == '\n' || e.KeyChar == '\r') && isShifted <= 0) {
				con.SendMessage (new LibGreen.Message () { Text = NewUserText.Text });
				ChatText.AppendText (this.con.UserName+": "+NewUserText.Text+"\n");
				NewUserText.Clear ();
			}
		}
	}
}
